package com.china08.yxyapi.schedule.lesson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudReportDistrict;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudReportSchool;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsCloudReportDistrictRepository;
import com.china08.yxyapi.repository.db.YxyLessonsCloudReportSchoolRepository;
import com.china08.yxyapi.util.YxyScheduleRunnableUtil;

@Component
@EnableScheduling
@SuppressWarnings("unchecked")
public class LessonCloudStatiscSchedule {

    @Value(value = "${yx.rest.api.yx.path}")
    String yxyApiYxUrl = "http://yuntest.91yunxiao.com/api_yx";

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    YxyLessonsCloudReportDistrictRepository cloudReportDistrictRepository;

    @Autowired
    YxyLessonsCloudReportSchoolRepository cloudReportSchoolRepository;

    public List<String> getCeShiSchool() {
        // 云校综合学校（哈尔滨）
        String sch1 = "ff8080815ba9422a015bb326acfa1599";
        // 哈尔滨演示学校
        String sch2 = "ff8080815ac67d12015b03a25cf70a5c";
        // 哈尔滨教育云（学校）
        String sch3 = "ff8080815a032de4015a3c0fdf2d7a16";
        // 哈尔滨教育学校
        String sch4 = "ff808081598c885c01599b2857f52de7";
        // 哈尔滨体验学校
        String sch5 = "ff80808148f98a490148fcdab1760008";
        List<String> ceshiSchoolIdList = new ArrayList<String>();
        ceshiSchoolIdList.add(sch1);
        ceshiSchoolIdList.add(sch2);
        ceshiSchoolIdList.add(sch3);
        ceshiSchoolIdList.add(sch4);
        ceshiSchoolIdList.add(sch5);
        return ceshiSchoolIdList;
    }

    // @Scheduled(fixedRate = 1000 * 60 * 10)
    @Scheduled(cron = "0 15 4 * * ?")
    public void execute1() {
        if (!YxyScheduleRunnableUtil.checkRunnable()) { return; }

        System.out.println("按区域统计开始");
        try {

            // 获取哈尔滨下面的所有县
            String apiUrl = yxyApiYxUrl + "/comm/area/230100";
            List<Map<String, Object>> areaList = restTemplate.getForObject(apiUrl, List.class);
            for (Map<String, Object> map : areaList) {
                // 县id
                String areaIdDistrict = map.get("area_id") + "";
                // 县名称
                String areaNameDistrict = map.get("area_name") + "";

                // 获取县下面所有学校
                String url = yxyApiYxUrl + "/comm/school/list/by/" + areaIdDistrict;
                List<Map<String, Object>> schoolMapList = restTemplate.getForObject(url, List.class);

                Integer lessonsCount = 0;
                Integer passCount = 0;
                Integer highQualityCount = 0;

                for (Map<String, Object> schoolMap : schoolMapList) {
                    String schoolId = schoolMap.get("id") + "";

                    // 是测试学校
                    if (getCeShiSchool().contains(schoolId)) {
                        continue;
                    }

                    StringBuffer sb1 = new StringBuffer();
                    sb1.append("  select                                         ");
                    sb1.append("    count(id)                                    ");
                    sb1.append("  from yxy_lessons.yxy_lessons_cloud_lesson      ");
                    sb1.append("  where status !=  - 1 and status != 0           ");
                    sb1.append("        and school_id =?                         ");
                    lessonsCount = lessonsCount + jdbcTemplate.queryForObject(sb1.toString(), new Object[] { schoolId }, Integer.class);

                    // 所有审核通过课程
                    StringBuffer sb2 = new StringBuffer();
                    sb2.append("  select                                         ");
                    sb2.append("    count(id)                                    ");
                    sb2.append("  from yxy_lessons.yxy_lessons_cloud_lesson      ");
                    sb2.append("  where status =  2                              ");
                    sb2.append("  and is_visible = 1                             ");
                    sb2.append("  and school_id = ?                              ");
                    passCount = passCount + jdbcTemplate.queryForObject(sb2.toString(), new Object[] { schoolId }, Integer.class);

                    // 所有精品课程
                    StringBuffer sb3 = new StringBuffer();
                    sb3.append("  select                                            ");
                    sb3.append("    count(id)                                       ");
                    sb3.append("  from yxy_lessons.yxy_lessons_cloud_lesson         ");
                    sb3.append("  where status = 2                                  ");
                    sb3.append("      and is_jingpin_lesson = 1                     ");
                    sb3.append("      and school_id = ?                             ");
                    highQualityCount = highQualityCount + jdbcTemplate.queryForObject(sb3.toString(), new Object[] { schoolId }, Integer.class);
                }
                YxyLessonsCloudReportDistrict clazz = cloudReportDistrictRepository.findByAreaIdDistrict(areaIdDistrict);
                if (clazz == null) {
                    clazz = new YxyLessonsCloudReportDistrict();
                    clazz.setAreaIdProvince("230000");
                    clazz.setAreaIdCity("230100");
                    clazz.setAreaIdDistrict(areaIdDistrict);
                    clazz.setAreaNameProvince("黑龙江省");
                    clazz.setAreaNameCity("哈尔滨市");
                    clazz.setAreaNameDistrict(areaNameDistrict);
                }
                clazz.setLessonsCount(lessonsCount);
                clazz.setPassCount(passCount);
                clazz.setHighQualityCount(highQualityCount);
                clazz = cloudReportDistrictRepository.save(clazz);
            }
        } catch (Exception e) {
        }
    }

    // @Scheduled(fixedRate = 1000 * 60 * 10)
    @Scheduled(cron = "0 15 4 * * ?")
    public void execute2() {

        if (!YxyScheduleRunnableUtil.checkRunnable()) { return; }

        System.out.println("按学校统计开始");

        try {
            // InetAddress address = InetAddress.getLocalHost();
            // String hostAddress = address.getHostAddress();
            // if (!hostAddress.equals("192.168.2.211")) { return; }

            //
            String apiUrl = yxyApiYxUrl + "/comm/school/list/230100";
            // 获取哈尔滨市下面的所有县所有
            List<Map<String, Object>> schoolMapList = restTemplate.getForObject(apiUrl, List.class);
            for (Map<String, Object> schoolMap : schoolMapList) {
                String schoolId = schoolMap.get("id") + "";
                String schoolName = schoolMap.get("school_nick") + "";
                String areaIdDistrict = schoolMap.get("area_id_district") + "";
                String areaNameDistrict = schoolMap.get("area_name_district") + "";
                String areaIdProvince = schoolMap.get("area_id_province") + "";
                String areaNameProvince = schoolMap.get("area_name_province") + "";
                String areaIdCity = schoolMap.get("area_id_city") + "";
                String areaNameCity = schoolMap.get("area_name_city") + "";

                // 是测试学校
                if (getCeShiSchool().contains(schoolId)) {
                    continue;
                }

                StringBuffer sb1 = new StringBuffer();
                sb1.append("  select                                         ");
                sb1.append("    count(id)                                    ");
                sb1.append("  from yxy_lessons.yxy_lessons_cloud_lesson      ");
                sb1.append("  where status !=  - 1 and status != 0           ");
                sb1.append("        and school_id =?                         ");
                Integer lessonsCount = jdbcTemplate.queryForObject(sb1.toString(), new Object[] { schoolId }, Integer.class);

                // 所有审核通过课程
                StringBuffer sb2 = new StringBuffer();
                sb2.append("  select                                         ");
                sb2.append("    count(id)                                    ");
                sb2.append("  from yxy_lessons.yxy_lessons_cloud_lesson      ");
                sb2.append("  where status =  2                              ");
                sb2.append("  and is_visible = 1                             ");
                sb2.append("  and school_id = ?                              ");
                Integer passCount = jdbcTemplate.queryForObject(sb2.toString(), new Object[] { schoolId }, Integer.class);

                // 所有精品课程
                StringBuffer sb3 = new StringBuffer();
                sb3.append("  select                                            ");
                sb3.append("    count(id)                                       ");
                sb3.append("  from yxy_lessons.yxy_lessons_cloud_lesson         ");
                sb3.append("  where status = 2                                  ");
                sb3.append("      and is_jingpin_lesson = 1                     ");
                sb3.append("      and school_id = ?                             ");
                Integer highQualityCount = jdbcTemplate.queryForObject(sb3.toString(), new Object[] { schoolId }, Integer.class);

                YxyLessonsCloudReportSchool clazz = cloudReportSchoolRepository.findBySchoolId(schoolId);
                if (clazz == null) {
                    clazz = new YxyLessonsCloudReportSchool();
                    clazz.setSchoolId(schoolId);
                    clazz.setSchoolName(schoolName);
                    clazz.setAreaIdDistrict(areaIdDistrict);
                    clazz.setAreaNameDistrict(areaNameDistrict);
                    clazz.setAreaIdCity(areaIdCity);
                    clazz.setAreaNameCity(areaNameCity);
                    clazz.setAreaIdProvince(areaIdProvince);
                    clazz.setAreaNameProvince(areaNameProvince);
                }
                clazz.setLessonsCount(lessonsCount);
                clazz.setPassCount(passCount);
                clazz.setHighQualityCount(highQualityCount);
                cloudReportSchoolRepository.save(clazz);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Pojo {
        public String schoolId;
        public String schoolNick;
        public String areaIdProvince;
        public String areaIdCity;
        public String areaIdDistrict;
    }

    protected static class RespModel extends YxyApiReqModel {
        public List<Pojo> list;
    }
}
